﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using PowerMonitor.BaseCtrl;

namespace PowerMonitor
{
    public partial class ShowChart : Form
    {
        private ShowChart()
        {
            InitializeComponent();
        }
        private int chlId = 0;
        private int devId = 0;
        private int itemId = 0;
        private int MaxNum = 10;
        BaseDevice _curDev = null;
        List<DevDataItem> _validItem;
        DevDataItem _curItem = null;
        public ShowChart(BaseDevice curdev, int chl, int dev, int item)
        {
            _validItem = new List<DevDataItem>();
            _curDev = curdev;
            chlId = chl;
            devId = dev;
            itemId = item;
            _curItem = AppMain.ChlList[chlId].GetDevByIndex(devId).GetDataItem(item);
            FillItemList(_curDev);
            InitializeComponent();
        }
        private void FillItemList(BaseDevice dev)
        {
            _validItem = dev.GetDataList();
        }
        private void Chart_Load(object sender, EventArgs e)
        {
            // Zoom into the X axis
            WaveRecChart.ChartAreas[0].AxisX.ScaleView.Zoom(1, 100);
            // Enable range selection and zooming end user interface
            // WaveRecChart.ChartAreas[0].CursorX.IsUserEnabled = true;
            WaveRecChart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
            WaveRecChart.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
            //将滚动内嵌到坐标轴中
            WaveRecChart.ChartAreas[0].AxisX.ScrollBar.IsPositionedInside = true;
            // 设置滚动条的大小
            WaveRecChart.ChartAreas[0].AxisX.ScrollBar.Size = 15;
            // 设置滚动条的按钮的风格，下面代码是将所有滚动条上的按钮都显示出来
            WaveRecChart.ChartAreas[0].AxisX.ScrollBar.ButtonStyle = System.Windows.Forms.DataVisualization.Charting.ScrollBarButtonStyles.All;
            // 设置自动放大与缩小的最小量
            WaveRecChart.ChartAreas[0].AxisX.ScaleView.SmallScrollSize = double.NaN;
            WaveRecChart.ChartAreas[0].AxisX.ScaleView.SmallScrollMinSize = 10;
            // Y 轴自适应 曲线在图形中间
            WaveRecChart.ChartAreas[0].AxisY.IsStartedFromZero = false;
            // WaveRecChart.ChartAreas[0].AxisX.LineWidth = 5;


            WaveRecChart.Series.Clear();
            WaveRecChart.ResetAutoValues();
            Series Item = new Series();
            Item.Color = Color.Green;
            //Item.ChartType = SeriesChartType.FastLine; // 大量数据时适合采用FastLine Or FastPoint     
            SetSeriesChartType(Item);
            Item.BorderWidth = 3;
            Item.MarkerColor = Color.Red;
            Item.MarkerStyle = MarkerStyle.Circle;
            Item.IsValueShownAsLabel = true;
            Item.Name = "曲线1";
            Item.XValueType = ChartValueType.DateTime;
            WaveRecChart.ChartAreas[0].AxisX.ScaleView.ZoomReset();
            WaveRecChart.ChartAreas[0].AxisX.Minimum = DateTime.Now.ToOADate();// 
            WaveRecChart.ChartAreas[0].AxisX.Maximum = DateTime.Now.AddSeconds((int)numDispMaxCnt.Value).ToOADate();
            WaveRecChart.ChartAreas[0].CursorX.IntervalType = DateTimeIntervalType.Seconds;
            WaveRecChart.ChartAreas[0].AxisX.LabelStyle.Format = "HH:mm:ss";  // 如果不设置格式 默认显示为日期 
            WaveRecChart.Series.Add(Item);
            MaxNum = _validItem.Count;
            if (MaxNum == 1)
            {
                tsmiNextItem.Enabled = false;
                tsmiPreviousItem.Enabled = false;
            }
            WaveRecChart.Titles[0].Text = _curItem.Name ;
        }

        private void SetSeriesChartType(Series Item)
        {
            if (Item==null)
            {
                return;
            }
            if (tsmiChartTypeFastline.Checked)
            {
                Item.ChartType = SeriesChartType.FastLine;
            }
            else if (tsmiChartTypeSpline.Checked)
            {
                Item.ChartType = SeriesChartType.Spline;
            }
            else if (tsmiChartTypeColumn.Checked)
            {
                Item.ChartType = SeriesChartType.Column;
            }
            else
            {
                Item.ChartType = SeriesChartType.FastLine;
            }
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (_bChangeItem)
            {
                _bChangeItem = false;
                WaveRecChart.Series.Clear();
                WaveRecChart.ResetAutoValues();
                Series Item = new Series();
                Item.Color = Color.Green;
                //Item.ChartType = SeriesChartType.FastLine; // 大量数据时适合采用FastLine Or FastPoint      
                SetSeriesChartType(Item);
                Item.BorderWidth = 3;
                Item.MarkerColor = Color.Red;
                Item.MarkerStyle = MarkerStyle.Circle;
                Item.IsValueShownAsLabel = true;
                Item.Name = "曲线1";
                Item.XValueType = ChartValueType.DateTime;
                WaveRecChart.ChartAreas[0].AxisX.ScaleView.ZoomReset();
                
                WaveRecChart.ChartAreas[0].AxisX.Minimum = DateTime.Now.ToOADate();
                WaveRecChart.ChartAreas[0].AxisX.Maximum =  DateTime.Now.AddSeconds((int)numDispMaxCnt.Value).ToOADate();
                WaveRecChart.ChartAreas[0].CursorX.IntervalType = DateTimeIntervalType.Minutes;
                WaveRecChart.Series.Add(Item);
                MaxNum = _validItem.Count;
                _curItem = _validItem[itemId];
                WaveRecChart.Titles[0].Text =  _curItem.Name ;
            }
            RealTimeDataShow();
        }

        private UInt32 _tickCnt = 2;
        private void RealTimeDataShow() // 显示实时曲线
        {
            if (WaveRecChart.Series.Count <= 0)
            {
                return;
            }
            UInt32 cycle = (UInt32)numCycle.Value;
            _tickCnt++; // 1s

            if (_tickCnt % cycle != 0)
            {
                return;
            }
            Series ptSeries = WaveRecChart.Series[0];
            DateTime timeStamp = DateTime.Now;
            txtCurValue.Text = _curItem.dCurValue.ToString(_curItem.strFormat);

            ptSeries.Points.AddXY(timeStamp.ToOADate(), _curItem.dCurValue);
            double removeBefore = timeStamp.AddSeconds((Double)(0 - numDispMaxCnt.Value)).ToOADate();
            //remove oldest values to maintain a constant number of data points
           
            while (ptSeries.Points[0].XValue < removeBefore)
            {
                ptSeries.Points.RemoveAt(0);
            }

            WaveRecChart.ChartAreas[0].AxisX.Minimum = ptSeries.Points[0].XValue;
            WaveRecChart.ChartAreas[0].AxisX.Maximum = DateTime.FromOADate(ptSeries.Points[0].XValue).AddSeconds((int)numDispMaxCnt.Value).ToOADate();

            // Redraw chart
            WaveRecChart.Invalidate();
        }

       

        private void tsmiShowZeroY_Click(object sender, EventArgs e)
        {
            if (tsmiShowZeroY.Checked)
            {
                WaveRecChart.ChartAreas[0].AxisY.IsStartedFromZero = true;
            }
            else
            {
                WaveRecChart.ChartAreas[0].AxisY.IsStartedFromZero = false;
            }
        }

        bool _bChangeItem = false;
        private void tsmiNextItem_Click(object sender, EventArgs e)
        {
            if (itemId < MaxNum - 1)
            {
                itemId++;
            }
            if (MaxNum - 1 == itemId)
            {
                tsmiNextItem.Enabled = false;
            }
            else
            {
                tsmiNextItem.Enabled = true;
            }
            if (0 != itemId)
            {
                tsmiPreviousItem.Enabled = true;
            }
            else
            {
                tsmiPreviousItem.Enabled = false;
            }
            _bChangeItem = true;
        }

        private void tsmiPreviousItem_Click(object sender, EventArgs e)
        {
            if (itemId > 0)
            {
                itemId--;
            }
            if (MaxNum - 1 == itemId)
            {
                tsmiNextItem.Enabled = false;
            }
            else
            {
                tsmiNextItem.Enabled = true;
            }
            if (0 != itemId)
            {
                tsmiPreviousItem.Enabled = true;
            }
            else
            {
                tsmiPreviousItem.Enabled = false;
            }
            _bChangeItem = true;
        }

        private void UpdateToolStripMenu(ToolStripMenuItem menu)
        {
            tsmiChartTypeFastline.Checked = false;
            tsmiChartTypeColumn.Checked = false;
            tsmiChartTypeSpline.Checked = false;
            menu.Checked = true;
        }
        private void tsmiChartTypeColumn_Click(object sender, EventArgs e)
        {
            if (WaveRecChart.Series.Count < 0)
            {
                return;
            }
            WaveRecChart.Series[0].ChartType = SeriesChartType.Column;
            UpdateToolStripMenu(tsmiChartTypeColumn);
        }
        private void tsmiChartTypeSpline_Click(object sender, EventArgs e)
        {
            if (WaveRecChart.Series.Count < 0)
            {
                return;
            }
            WaveRecChart.Series[0].ChartType = SeriesChartType.Spline;  // 显示数据点   
            UpdateToolStripMenu(tsmiChartTypeSpline);
        }
        private void tsmiChartTypeFastline_Click(object sender, EventArgs e)
        {
            if (WaveRecChart.Series.Count < 0)
            {
                return;
            }
            WaveRecChart.Series[0].ChartType = SeriesChartType.FastLine; // 大量数据时适合采用FastLine Or FastPoint  
            UpdateToolStripMenu(tsmiChartTypeFastline);
        }
    }
}
